package com.darwin.darwinUtils
{
	import flash.filters.ColorMatrixFilter;
	import flash.filters.DropShadowFilter;
	import flash.geom.Matrix;

	/**
	 *	创建用滤镜工具类 
	 * @author Roland
	 * 
	 */
	public class FilterUtil
	{
		private static var _idMatrix:Array = [1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0];
		private static var _lumR:Number = 0.212671; //Red 对比 
		private static var _lumG:Number = 0.715160; //Green 对比 
		private static var _lumB:Number = 0.072169; //Blue 对比
		public function FilterUtil()
		{
		}
		
		/**
		 *	创建一个颜色矩阵滤镜 
		 * @param brightness 亮度
		 * @param contrast 对比度
		 * @param saturation 饱和度
		 * @param hue 色相
		 * @return 
		 * 
		 */
		public static function createColorMatrixFilter(brightness:Number,contrast:Number,saturation:Number,hue:Number):ColorMatrixFilter
		{
			var matrix:Array = new ColorMatrix();
			matrix.adjustColor(brightness,contrast,saturation,hue);
			/*var endMatrix:Array =  _idMatrix.slice();
			endMatrix = setBrightness(endMatrix, (brightness + 100)/100);
			endMatrix = setContrast(endMatrix, contrast,72);//
			endMatrix = setSaturation(endMatrix, (saturation + 50)/50);
			endMatrix = setHue(endMatrix, hue);*/
			var c:ColorMatrixFilter = new ColorMatrixFilter(matrix);
			return c;
		}
		
		
		/**
		 * 	创建一个彩色投影滤镜数组
		 * @param color 投影颜色
		 * @param strength 投影强度
		 * @param alpha 投影alpha
		 * @param angle 投影角度
		 * @param distance 投影距离
		 * @param blurX 模糊x
		 * @param blurY 模糊y
		 * @return [指定颜色的投影滤镜]
		 * 
		 */
		public static function createDSFilter(color:uint=0x003366,strength:Number = 6.0,alpha:Number = 1,angle:Number = 0,distance:Number = 0.0,blurX:Number =  5.0,blurY:Number = 5.0):Array
		{
			var inner:Boolean = false;
			var knockout:Boolean = false;
			var quality:Number = 1;
			var hideObject:Boolean = false;
			return [new DropShadowFilter(distance,angle,color,alpha,blurX,blurY,strength,quality,inner,knockout,hideObject)];
		}
		
		/**
		 *	设置阀值 
		 */
		private static function setThreshold(m:Array, n:Number):Array {
			if (isNaN(n)) {
				return m;
			}
			var temp:Array = [_lumR * 256, _lumG * 256, _lumB * 256, 0,  -256 * n, 
				_lumR * 256, _lumG * 256, _lumB * 256, 0,  -256 * n, 
				_lumR * 256, _lumG * 256, _lumB * 256, 0,  -256 * n, 
				0,           0,           0,           1,  0]; 
			return applyMatrix(temp, m);
		}
		
		/**
		 *	设置色相
		 */
		private static function setHue(m:Array, n:Number):Array {
			if (isNaN(n)) {
				return m;
			}
			n *= Math.PI / 180;
			var c:Number = Math.cos(n);
			var s:Number = Math.sin(n);
			var temp:Array = [(_lumR + (c * (1 - _lumR))) + (s * (-_lumR)), (_lumG + (c * (-_lumG))) + (s * (-_lumG)), (_lumB + (c * (-_lumB))) + (s * (1 - _lumB)), 0, 0, (_lumR + (c * (-_lumR))) + (s * 0.143), (_lumG + (c * (1 - _lumG))) + (s * 0.14), (_lumB + (c * (-_lumB))) + (s * -0.283), 0, 0, (_lumR + (c * (-_lumR))) + (s * (-(1 - _lumR))), (_lumG + (c * (-_lumG))) + (s * _lumG), (_lumB + (c * (1 - _lumB))) + (s * _lumB), 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1];
			return applyMatrix(temp, m);
		}
		
		/**
		 *	设置亮度
		 */
		private static function setBrightness(m:Array, n:Number):Array {
			if (isNaN(n)) {
				return m;
			}
			n = (n * 100) - 100;
			return applyMatrix([1,0,0,0,n,
				0,1,0,0,n,
				0,0,1,0,n,
				0,0,0,1,0,
				0,0,0,0,1], m);
		}
		
		/**
		 *	设置饱和度
		 */
		private static function setSaturation(m:Array, n:Number):Array {
			if (isNaN(n)) {
				return m;
			}
			var inv:Number = 1 - n;
			var r:Number = inv * _lumR;
			var g:Number = inv * _lumG;
			var b:Number = inv * _lumB;
			var temp:Array = [r + n, g     , b     , 0, 0,
				r     , g + n, b     , 0, 0,
				r     , g     , b + n, 0, 0,
				0     , 0     , 0     , 1, 0];
			return applyMatrix(temp, m);
		}
		
		/**
		 *	设置对比度 
		 * @param m 要设置的矩阵数组
		 * @param n 对比度值
		 * @param t 阀值
		 * @return 
		 * 
		 */
		public static function setContrast(m:Array, n:Number,t:Number = 128):Array {
			if (isNaN(n)) {
				return m;
			}
			n += 0.01;
			var temp:Array =  [n,0,0,0,t * (1 - n),
				0,n,0,0,t * (1 - n),
				0,0,n,0,t * (1 - n),
				0,0,0,1,0];
			return applyMatrix(temp, m);
		}
		
		/** 应用矩阵变换 **/
		public static function applyMatrix(m:Array, m2:Array):Array {
			if (!(m is Array) || !(m2 is Array)) {
				return m2;
			}
			var temp:Array = [], i:int = 0, z:int = 0, y:int, x:int;
			for (y = 0; y < 4; y += 1) {
				for (x = 0; x < 5; x += 1) {
					if (x == 4) {
						z = m[i + 4];
					} else {
						z = 0;
					}
					temp[i + x] = m[i]   * m2[x]      + 
						m[i+1] * m2[x + 5]  + 
						m[i+2] * m2[x + 10] + 
						m[i+3] * m2[x + 15] +
						z;
				}
				i += 5;
			}
			return temp;
		}
	}
}